package com.mall.server.controller;

import com.mall.commons.R;
import com.mall.commons.utils.JwtUtils;
import com.mall.server.inerceptor.UmsAdminHolder;
import com.mall.server.service.AdminService;

import com.tanhua.model.domain.UmsAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @PostMapping("/login")
    public R login(@RequestBody UmsAdmin umsAdmin) {
        if (umsAdmin.getPassword() == null || umsAdmin.getUsername() == null){
            return R.error("账号密码输入格式不正确");
        }

        UmsAdmin login = adminService.login(umsAdmin.getUsername(), umsAdmin.getPassword());
        if (login ==null){
            return R.error("账号密码输入格式不正确,请重新输入");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",login.getId());
        map.put("username",login.getUsername());
        String token = JwtUtils.getToken(map);

        HashMap<String, String> map2= new HashMap<>();
        map2.put("token",token);

        return R.success(map2);
    }


    /**
     * 返回值
     * {
     *     "code":200,
     *     "message":"操作成功",
     *     "data":{
     *         "roles":Array[1],
     *         "icon":"http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180607/timg.jpg",
     *         "menus":Array[23],
     *         "username":"admin"
     *     }
     * }
     * @return
     */
    @GetMapping("/info")
    public R info(){
        Map map =adminService.info();
        return R.success(map);

    }


}
